package com.xbf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbf.entity.Goods;
import com.xbf.mapper.GoodsMapper;
import com.xbf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional
    public boolean reduceStock(Integer goodsId, Integer quantity) {
        Goods goods = getById(goodsId);
        if (goods == null || goods.getGNum() < quantity) {
            return false;
        }
        goods.setGNum(goods.getGNum() - quantity);
        return updateById(goods);
    }

    // 实现获取所有商品的方法
    @Override
    public List<Goods> getAllGoods() {
        // 使用 MyBatis Plus 提供的 selectList 方法
        return baseMapper.selectList(null);
    }
}